package com.caisin.mybatis.generator.plugin;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.*;

public class AddAnnotationsPlugin extends PluginAdapter {
    private Set<String> annotations= new HashSet<>();
    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }
    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        String annotations = this.properties.getProperty("annotations");
        if (StringUtility.stringHasValue(annotations)) {
            Collections.addAll(this.annotations, annotations.split(","));
        } else {
            throw new RuntimeException("AddAnnotations插件缺少必要的annotations属性!");
        }
    }
    /**
     * 生成基础实体类
     *
     * @param topLevelClass
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        for (String annotation : annotations) {
            topLevelClass.addImportedType(annotation);
            String className = annotation.substring(annotation.lastIndexOf(".") + 1);
            topLevelClass.addAnnotation("@"+className);
        }
        return true;
    }
}
